package NewTmas.FramesandPanels;

import NewTmas.ClickAction;
import NewTmas.EditMode.EditModeMainFrame;
import taskmaster.auth.Login;
import taskmaster.auth.LoginTools;
import taskmaster.auth.User;
import taskmaster.job.TaskBuilder;
import taskmaster.util.LookupLists;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;


public class LoginFrame extends LoginTools implements ActionListener
{

    private final JFrame loginframe;
    private final JTextField namefield;
    private final JPasswordField pwdfield;
    private User u;
    private Login l;
    private LookupLists ll;
    private TaskBuilder tb;

    private final JComboBox comboBox;
    private final JComboBox cbox2;

    /**
     * Launch the application.
     */
    public static void main(String[] args)
    {
        printClassLoaderAndEnv();
        try
        {
            EventQueue.invokeLater(new Runnable()
            {
                public void run()
                {
                    LoginFrame window = new LoginFrame("", "", "tman.qa");
                }
            });
        } catch (Exception e)
        {
            JOptionPane.showMessageDialog(null, "Could not login : " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
            e.printStackTrace();
        }
    }

    /**
     * Create the application.
     *
     * @param userName
     * @param password
     * @param db
     */
    public LoginFrame(final String userName, final String password, final String db)
    {

        loginframe = new JFrame();
        loginframe.getContentPane().setBackground(Color.WHITE);
        loginframe.getContentPane().setForeground(Color.BLACK);
        loginframe.setTitle("NEWTMS");
        loginframe.setBounds(100, 100, 535, 555);
        loginframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        loginframe.getContentPane().setLayout(null);

        namefield = new JTextField();
        namefield.setText(userName);
        namefield.setBounds(224, 137, 117, 20);
        loginframe.getContentPane().add(namefield);
        namefield.setColumns(10);

        JLabel lblUsername = new JLabel("Oracle UserName");
        lblUsername.setBounds(66, 139, 160, 17);
        loginframe.getContentPane().add(lblUsername);

        JLabel lblPassword = new JLabel("Password");
        lblPassword.setBounds(66, 218, 148, 14);
        loginframe.getContentPane().add(lblPassword);


        JLabel lblJobMonitorSystem = new JLabel("JOB MONITOR SYSTEM");
        lblJobMonitorSystem.setFont(new Font("Tahoma", Font.BOLD, 15));
        lblJobMonitorSystem.setForeground(new Color(0, 0, 0));
        lblJobMonitorSystem.setBounds(128, 11, 185, 14);

        JButton submit = new JButton("Submit");
        submit.setBackground(Color.WHITE);
        submit.setBounds(224, 446, 89, 23);
        loginframe.getContentPane().add(submit);

        pwdfield = new JPasswordField();
        pwdfield.setBounds(224, 215, 117, 20);
        pwdfield.setText(password);
        loginframe.getContentPane().add(pwdfield);


        comboBox = new JComboBox();
        comboBox.setBounds(224, 279, 117, 20);
        loginframe.getContentPane().add(comboBox);
        comboBox.addItem("tman.prod");
        comboBox.addItem("tman.qa");
        comboBox.addItem("tms.dr");
        //TODO:read a default db from properties/environment!!
        comboBox.addItem("tman.hq");
        cbox2 = new JComboBox();
        cbox2.setBounds(224, 343, 117, 20);
        cbox2.addItem("Ops Mode");
        cbox2.addItem("Edit Mode");
        loginframe.getContentPane().add(cbox2);


        if (db != null)
        {
            comboBox.setSelectedItem(db);
        }

        JLabel lblOperationalDatabase = new JLabel("Operational Database");
        lblOperationalDatabase.setBounds(66, 282, 125, 14);
        loginframe.getContentPane().add(lblOperationalDatabase);


        JLabel lblMode = new JLabel("Mode");
        lblMode.setBounds(66, 343, 125, 14);
        loginframe.getContentPane().add(lblMode);

        submit.addActionListener(this);
        loginframe.setVisible(true);


        submit.setMnemonic(KeyEvent.VK_ENTER);


        InputMap inputMap = submit.getInputMap(JButton.WHEN_IN_FOCUSED_WINDOW);
        KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
        inputMap.put(enter, "ENTER");
        submit.getActionMap().put("ENTER", new ClickAction(submit));


        submit.registerKeyboardAction(submit.getActionForKeyStroke(
                KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0, false)),
                KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false),
                JComponent.WHEN_FOCUSED);

        submit.registerKeyboardAction(submit.getActionForKeyStroke(
                KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0, true)),
                KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, true),
                JComponent.WHEN_FOCUSED);


    }

    public void actionPerformed(ActionEvent e)
    {

        String username = namefield.getText();
        String pwd = pwdfield.getText();

        if (username == null || pwd == null)
        {
            JOptionPane.showMessageDialog(loginframe, "Incorrect Username/Password", "Login Failed !", 0);
        } else
        {
            u = new User();
            l = new Login();
        }

        try
        {
            if (l.login(u, username, pwd, "oci8", comboBox.getSelectedItem().toString()))
            {
                // System.out.println("logged :session begins");

            }
        } catch (Exception e1)
        {
            JOptionPane.showMessageDialog(loginframe, "Problem establishing login for user: " + e1.getMessage(), "Login Failed !", 0);
        }

        try
        {
            ll = new LookupLists(u);
            tb = new TaskBuilder(ll);

        } catch (Exception ex)
        {
            JOptionPane.showMessageDialog(null, "Error initialising TaskBuilder and LookupLists objects: " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }


        try
        {
            if (cbox2.getSelectedItem().toString().equals("Ops Mode"))
            {
                OpsModeMainFrame mainFrame = new OpsModeMainFrame(u, ll, tb);
                mainFrame.setVisible(true);

            } else
            {

                EditModeMainFrame editModeMainFrame = new EditModeMainFrame(u, ll);

            }

            loginframe.setVisible(false);
        }
        catch (Exception ex)
        {

            JOptionPane.showMessageDialog(null, "Possible problem launching the main frame: " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);

        }
    }


}



